package com.chenl.provider.DEMOS.多线程相关;

import java.util.concurrent.*;

/**
 * 使用多线程的方式Demo
 */
public class AsyncDemoController {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        Future<?> callFuture = executorService.submit(() -> AsyncMethod("张三"));//lambda代替Callable
        Future<?> runFuture = executorService.submit(() -> AsyncMethod2("李四"));//lambda代替Runnable
        Future<?> runClassFuture = executorService.submit(new MyRunnable("王五"));//传入runnable对象
        Future<?> callClassFuture = executorService.submit(new MyCallable("赵六"));//传入callable对象
        executorService.shutdown();
        System.out.println("结束");
    }

    public static String AsyncMethod(String name) throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("AsyncMethod的名字是"+name);
        return "ok";
    }
    public static void AsyncMethod2(String name)   {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("AsyncMethod2的名字是"+name);
    }

    public static class MyRunnable implements Runnable{
        private String name;
        public MyRunnable(String name){
            this.name=name;
        }
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("Runnable的名字是:"+name);
        }
    }
    public static class MyCallable implements Callable {
        private String name;
        public MyCallable(String name){
            this.name=name;
        }

        @Override
        public Object call() throws Exception {
            Thread.sleep(1000);
            System.out.println("Callable的名字是:"+name);
            return "ok";
        }
    }



}

